<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>✅Linux shell使用笔记 | whoway</title>
    <meta name="description" content="Personal Blog Website">
    <link rel="icon" href="/images/photo.jpg">
  <link rel="manifest" href="/images/photo.jpg">
  <link rel="apple-touch-icon" href="/images/photo.jpg">
  <meta http-quiv="pragma" cotent="no-cache">
  <meta http-quiv="pragma" cotent="no-cache,must-revalidate">
  <meta http-quiv="expires" cotent="0">
    
    <link rel="preload" href="/assets/css/0.styles.0dbae9ec.css" as="style"><link rel="preload" href="/assets/js/app.c70e21ad.js" as="script"><link rel="preload" href="/assets/js/149.ffc835b5.js" as="script"><link rel="prefetch" href="/assets/js/10.15222a53.js"><link rel="prefetch" href="/assets/js/100.7e0e5a86.js"><link rel="prefetch" href="/assets/js/101.efd59f25.js"><link rel="prefetch" href="/assets/js/102.dfbdc06c.js"><link rel="prefetch" href="/assets/js/103.d3ab2109.js"><link rel="prefetch" href="/assets/js/104.117957ef.js"><link rel="prefetch" href="/assets/js/105.046e8ff3.js"><link rel="prefetch" href="/assets/js/106.aebdc17d.js"><link rel="prefetch" href="/assets/js/107.248733c2.js"><link rel="prefetch" href="/assets/js/108.a2fecadc.js"><link rel="prefetch" href="/assets/js/109.35196857.js"><link rel="prefetch" href="/assets/js/11.770642f2.js"><link rel="prefetch" href="/assets/js/110.cf3d973c.js"><link rel="prefetch" href="/assets/js/111.f985889a.js"><link rel="prefetch" href="/assets/js/112.ad614f41.js"><link rel="prefetch" href="/assets/js/113.f666653c.js"><link rel="prefetch" href="/assets/js/114.c6c3f384.js"><link rel="prefetch" href="/assets/js/115.e51d3c2f.js"><link rel="prefetch" href="/assets/js/116.4f4b39f5.js"><link rel="prefetch" href="/assets/js/117.99352e11.js"><link rel="prefetch" href="/assets/js/118.c6ae6572.js"><link rel="prefetch" href="/assets/js/119.4ccbe778.js"><link rel="prefetch" href="/assets/js/12.042a92ff.js"><link rel="prefetch" href="/assets/js/120.edda1c4f.js"><link rel="prefetch" href="/assets/js/121.30a638ed.js"><link rel="prefetch" href="/assets/js/122.6efcefb1.js"><link rel="prefetch" href="/assets/js/123.91e6665b.js"><link rel="prefetch" href="/assets/js/124.f27e3d7e.js"><link rel="prefetch" href="/assets/js/125.c75712d5.js"><link rel="prefetch" href="/assets/js/126.ed756cce.js"><link rel="prefetch" href="/assets/js/127.2f06c74c.js"><link rel="prefetch" href="/assets/js/128.d5f6f30e.js"><link rel="prefetch" href="/assets/js/129.508b7eed.js"><link rel="prefetch" href="/assets/js/13.b5280c37.js"><link rel="prefetch" href="/assets/js/130.dc05f9aa.js"><link rel="prefetch" href="/assets/js/131.e0ba69b1.js"><link rel="prefetch" href="/assets/js/132.d79bcaa4.js"><link rel="prefetch" href="/assets/js/133.34acc01a.js"><link rel="prefetch" href="/assets/js/134.dabf64d5.js"><link rel="prefetch" href="/assets/js/135.ad90c915.js"><link rel="prefetch" href="/assets/js/136.dbb0074f.js"><link rel="prefetch" href="/assets/js/137.284ad365.js"><link rel="prefetch" href="/assets/js/138.a4b6856f.js"><link rel="prefetch" href="/assets/js/139.c9c1e20f.js"><link rel="prefetch" href="/assets/js/14.df02ba38.js"><link rel="prefetch" href="/assets/js/140.8b0a9269.js"><link rel="prefetch" href="/assets/js/141.9c7759c5.js"><link rel="prefetch" href="/assets/js/142.a4201a82.js"><link rel="prefetch" href="/assets/js/143.d7da6e8c.js"><link rel="prefetch" href="/assets/js/144.5e48e65d.js"><link rel="prefetch" href="/assets/js/145.a0e2633c.js"><link rel="prefetch" href="/assets/js/146.3c775f9b.js"><link rel="prefetch" href="/assets/js/147.22add89a.js"><link rel="prefetch" href="/assets/js/148.cfde1009.js"><link rel="prefetch" href="/assets/js/15.fbdfc4ee.js"><link rel="prefetch" href="/assets/js/150.406c4b20.js"><link rel="prefetch" href="/assets/js/151.b2040eea.js"><link rel="prefetch" href="/assets/js/152.7bc65661.js"><link rel="prefetch" href="/assets/js/153.1d7c65e3.js"><link rel="prefetch" href="/assets/js/154.1309de49.js"><link rel="prefetch" href="/assets/js/155.81d3ee1f.js"><link rel="prefetch" href="/assets/js/156.154a4ef2.js"><link rel="prefetch" href="/assets/js/16.e5eb6147.js"><link rel="prefetch" href="/assets/js/17.57853c4a.js"><link rel="prefetch" href="/assets/js/18.cb9d7518.js"><link rel="prefetch" href="/assets/js/19.f354dc47.js"><link rel="prefetch" href="/assets/js/2.570d8a23.js"><link rel="prefetch" href="/assets/js/20.b5af7fad.js"><link rel="prefetch" href="/assets/js/21.0b1928fe.js"><link rel="prefetch" href="/assets/js/22.f78666de.js"><link rel="prefetch" href="/assets/js/23.29c3f366.js"><link rel="prefetch" href="/assets/js/24.6f596516.js"><link rel="prefetch" href="/assets/js/25.14067b60.js"><link rel="prefetch" href="/assets/js/26.74ba4989.js"><link rel="prefetch" href="/assets/js/27.13d60edd.js"><link rel="prefetch" href="/assets/js/28.9523cb32.js"><link rel="prefetch" href="/assets/js/29.8ec842e9.js"><link rel="prefetch" href="/assets/js/3.3fb3d2e0.js"><link rel="prefetch" href="/assets/js/30.805597a8.js"><link rel="prefetch" href="/assets/js/31.831b195d.js"><link rel="prefetch" href="/assets/js/32.063c672d.js"><link rel="prefetch" href="/assets/js/33.6d93fac3.js"><link rel="prefetch" href="/assets/js/34.56e8263c.js"><link rel="prefetch" href="/assets/js/35.dbe688bb.js"><link rel="prefetch" href="/assets/js/36.dc5af2c1.js"><link rel="prefetch" href="/assets/js/37.0a7494f6.js"><link rel="prefetch" href="/assets/js/38.fe4fc171.js"><link rel="prefetch" href="/assets/js/39.f5ed5e92.js"><link rel="prefetch" href="/assets/js/4.2c405ec8.js"><link rel="prefetch" href="/assets/js/40.fe7e2714.js"><link rel="prefetch" href="/assets/js/41.30b0811d.js"><link rel="prefetch" href="/assets/js/42.76f52d62.js"><link rel="prefetch" href="/assets/js/43.e7bb0817.js"><link rel="prefetch" href="/assets/js/44.ead0e883.js"><link rel="prefetch" href="/assets/js/45.235df046.js"><link rel="prefetch" href="/assets/js/46.5f09e829.js"><link rel="prefetch" href="/assets/js/47.67116354.js"><link rel="prefetch" href="/assets/js/48.31f34543.js"><link rel="prefetch" href="/assets/js/49.10b5ebba.js"><link rel="prefetch" href="/assets/js/5.6f47322c.js"><link rel="prefetch" href="/assets/js/50.c0f0b7f1.js"><link rel="prefetch" href="/assets/js/51.5143f3bf.js"><link rel="prefetch" href="/assets/js/52.eeddfd48.js"><link rel="prefetch" href="/assets/js/53.eb790db5.js"><link rel="prefetch" href="/assets/js/54.8fe5421c.js"><link rel="prefetch" href="/assets/js/55.d8f9004b.js"><link rel="prefetch" href="/assets/js/56.62ac9b92.js"><link rel="prefetch" href="/assets/js/57.a9caec0d.js"><link rel="prefetch" href="/assets/js/58.f93fc522.js"><link rel="prefetch" href="/assets/js/59.a81a03aa.js"><link rel="prefetch" href="/assets/js/6.8c2ea393.js"><link rel="prefetch" href="/assets/js/60.ab782775.js"><link rel="prefetch" href="/assets/js/61.6dd12daf.js"><link rel="prefetch" href="/assets/js/62.76f4b01f.js"><link rel="prefetch" href="/assets/js/63.6f8a4742.js"><link rel="prefetch" href="/assets/js/64.6f8bb1fa.js"><link rel="prefetch" href="/assets/js/65.4120a44b.js"><link rel="prefetch" href="/assets/js/66.360c2d2b.js"><link rel="prefetch" href="/assets/js/67.26f84d32.js"><link rel="prefetch" href="/assets/js/68.68f45e5e.js"><link rel="prefetch" href="/assets/js/69.e311eb56.js"><link rel="prefetch" href="/assets/js/7.6762b2d7.js"><link rel="prefetch" href="/assets/js/70.cea82674.js"><link rel="prefetch" href="/assets/js/71.783ddcf7.js"><link rel="prefetch" href="/assets/js/72.e5467385.js"><link rel="prefetch" href="/assets/js/73.b8fb681b.js"><link rel="prefetch" href="/assets/js/74.1bae37db.js"><link rel="prefetch" href="/assets/js/75.024387e5.js"><link rel="prefetch" href="/assets/js/76.a8e53010.js"><link rel="prefetch" href="/assets/js/77.8c55500a.js"><link rel="prefetch" href="/assets/js/78.7ce90bf5.js"><link rel="prefetch" href="/assets/js/79.ef71713f.js"><link rel="prefetch" href="/assets/js/8.788a6364.js"><link rel="prefetch" href="/assets/js/80.acad589d.js"><link rel="prefetch" href="/assets/js/81.02670d10.js"><link rel="prefetch" href="/assets/js/82.53b7b1ac.js"><link rel="prefetch" href="/assets/js/83.99eb8581.js"><link rel="prefetch" href="/assets/js/84.d1535ce3.js"><link rel="prefetch" href="/assets/js/85.fe2b7de9.js"><link rel="prefetch" href="/assets/js/86.41850272.js"><link rel="prefetch" href="/assets/js/87.1cdc6df9.js"><link rel="prefetch" href="/assets/js/88.01bf3461.js"><link rel="prefetch" href="/assets/js/89.17c69819.js"><link rel="prefetch" href="/assets/js/9.3813842d.js"><link rel="prefetch" href="/assets/js/90.f6ae7e35.js"><link rel="prefetch" href="/assets/js/91.507bc284.js"><link rel="prefetch" href="/assets/js/92.90551782.js"><link rel="prefetch" href="/assets/js/93.dc442d78.js"><link rel="prefetch" href="/assets/js/94.315f4e94.js"><link rel="prefetch" href="/assets/js/95.ccd6c6bf.js"><link rel="prefetch" href="/assets/js/96.0c6d89d0.js"><link rel="prefetch" href="/assets/js/97.1a9f10a9.js"><link rel="prefetch" href="/assets/js/98.43be3caa.js"><link rel="prefetch" href="/assets/js/99.54c8207b.js">
    <link rel="stylesheet" href="/assets/css/0.styles.0dbae9ec.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">whoway</span></a> <div class="links" style="max-width:nullpx;"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">🎓Coding</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/00.Coding/TheBeautyOfProgramming/" class="nav-link">🔖编程之美题解</a></li><li class="dropdown-item"><!----> <a href="/00.Coding/CodeWarehouse/" class="nav-link">🔖代码意识流</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">🚀语言</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/01.Language/Overview/" class="nav-link">🔖概述</a></li><li class="dropdown-item"><!----> <a href="/01.Language/C/" class="nav-link">⭐️C</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Cpp/" class="nav-link">🚀C++</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Java/" class="nav-link">☕️Java</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Python/" class="nav-link">🧩Python3</a></li></ul></div></div><div class="nav-item"><a href="/02.Hardware/" class="nav-link">✔️硬件基础</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">⭐️软件基础</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/03.Software/01.DataStructureAndAlgorithm/" class="nav-link">🐾数据结构和算法</a></li><li class="dropdown-item"><!----> <a href="/03.Software/02.OS/" class="nav-link">💻操作系统</a></li><li class="dropdown-item"><!----> <a href="/03.Software/03.Net/" class="nav-link">☁️计算机网络</a></li><li class="dropdown-item"><!----> <a href="/03.Software/04.SE/" class="nav-link">✅软件工程</a></li></ul></div></div><div class="nav-item"><a href="/04.Database/" class="nav-link">🎨数据库</a></div><div class="nav-item"><a href="/05.Engineer/" class="nav-link">🔖学术/工程</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">⚙️工具</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/06.Tools/01.employment/" class="nav-link">🔖求职</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/02.efficiency/" class="nav-link">🚀效能</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/03.windows/" class="nav-link">⚙️Windows</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/04.design/" class="nav-link">🧩设计</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/05.linux/" class="nav-link router-link-active">🐉Linux</a></li></ul></div></div><div class="nav-item"><a href="https://github.com/whoway" target="_blank" rel="noopener noreferrer" class="nav-link external">
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar"><nav class="nav-links"><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">🎓Coding</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/00.Coding/TheBeautyOfProgramming/" class="nav-link">🔖编程之美题解</a></li><li class="dropdown-item"><!----> <a href="/00.Coding/CodeWarehouse/" class="nav-link">🔖代码意识流</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">🚀语言</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/01.Language/Overview/" class="nav-link">🔖概述</a></li><li class="dropdown-item"><!----> <a href="/01.Language/C/" class="nav-link">⭐️C</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Cpp/" class="nav-link">🚀C++</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Java/" class="nav-link">☕️Java</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Python/" class="nav-link">🧩Python3</a></li></ul></div></div><div class="nav-item"><a href="/02.Hardware/" class="nav-link">✔️硬件基础</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">⭐️软件基础</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/03.Software/01.DataStructureAndAlgorithm/" class="nav-link">🐾数据结构和算法</a></li><li class="dropdown-item"><!----> <a href="/03.Software/02.OS/" class="nav-link">💻操作系统</a></li><li class="dropdown-item"><!----> <a href="/03.Software/03.Net/" class="nav-link">☁️计算机网络</a></li><li class="dropdown-item"><!----> <a href="/03.Software/04.SE/" class="nav-link">✅软件工程</a></li></ul></div></div><div class="nav-item"><a href="/04.Database/" class="nav-link">🎨数据库</a></div><div class="nav-item"><a href="/05.Engineer/" class="nav-link">🔖学术/工程</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">⚙️工具</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/06.Tools/01.employment/" class="nav-link">🔖求职</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/02.efficiency/" class="nav-link">🚀效能</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/03.windows/" class="nav-link">⚙️Windows</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/04.design/" class="nav-link">🧩设计</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/05.linux/" class="nav-link router-link-active">🐉Linux</a></li></ul></div></div><div class="nav-item"><a href="https://github.com/whoway" target="_blank" rel="noopener noreferrer" class="nav-link external">
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav>  <ul class="sidebar-links"><li><div class="sidebar-group first"><p class="sidebar-heading open"><span>✅Linux shell使用笔记</span> <!----></p> <ul class="sidebar-group-items"><li><a href="/06.Tools/05.linux/Part03.Shell%E8%84%9A%E6%9C%AC.html#_01-判断的条件" class="sidebar-link">01.判断的条件</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/06.Tools/05.linux/Part03.Shell%E8%84%9A%E6%9C%AC.html#格式1" class="sidebar-link">格式1</a></li><li class="sidebar-sub-header"><a href="/06.Tools/05.linux/Part03.Shell%E8%84%9A%E6%9C%AC.html#格式2" class="sidebar-link">格式2</a></li></ul></li><li><a href="/06.Tools/05.linux/Part03.Shell%E8%84%9A%E6%9C%AC.html#_02-每次的习惯" class="sidebar-link">02.每次的习惯</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/06.Tools/05.linux/Part03.Shell%E8%84%9A%E6%9C%AC.html#_03-测试环境" class="sidebar-link">03.测试环境</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/06.Tools/05.linux/Part03.Shell%E8%84%9A%E6%9C%AC.html#_04-常见shell错误" class="sidebar-link">04.常见shell错误</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/06.Tools/05.linux/Part03.Shell%E8%84%9A%E6%9C%AC.html#_05-常见命令" class="sidebar-link">05.常见命令</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/06.Tools/05.linux/Part03.Shell%E8%84%9A%E6%9C%AC.html#_06-if的使用" class="sidebar-link">06.if的使用</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/06.Tools/05.linux/Part03.Shell%E8%84%9A%E6%9C%AC.html#_07-for的使用" class="sidebar-link">07.for的使用</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/06.Tools/05.linux/Part03.Shell%E8%84%9A%E6%9C%AC.html#格式1-2" class="sidebar-link">格式1</a></li><li class="sidebar-sub-header"><a href="/06.Tools/05.linux/Part03.Shell%E8%84%9A%E6%9C%AC.html#格式2-2" class="sidebar-link">格式2</a></li></ul></li><li><a href="/06.Tools/05.linux/Part03.Shell%E8%84%9A%E6%9C%AC.html#_08-while的使用" class="sidebar-link">08.while的使用</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/06.Tools/05.linux/Part03.Shell%E8%84%9A%E6%9C%AC.html#格式" class="sidebar-link">格式</a></li><li class="sidebar-sub-header"><a href="/06.Tools/05.linux/Part03.Shell%E8%84%9A%E6%9C%AC.html#无限循环" class="sidebar-link">无限循环</a></li></ul></li><li><a href="/06.Tools/05.linux/Part03.Shell%E8%84%9A%E6%9C%AC.html#_09-until循环（有点像c语言下的do-while循环）" class="sidebar-link">09.until循环（有点像C语言下的do while循环）</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/06.Tools/05.linux/Part03.Shell%E8%84%9A%E6%9C%AC.html#_10-case多选择（类似于c语言中的switch选择）" class="sidebar-link">10.case多选择（类似于C语言中的switch选择）</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/06.Tools/05.linux/Part03.Shell%E8%84%9A%E6%9C%AC.html#_11-break和continue" class="sidebar-link">11.break和continue</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/06.Tools/05.linux/Part03.Shell%E8%84%9A%E6%9C%AC.html#_12-shell-基本运算符（记忆、强化，对比汇编语句）" class="sidebar-link">12.Shell 基本运算符（记忆、强化，对比汇编语句）</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/06.Tools/05.linux/Part03.Shell%E8%84%9A%E6%9C%AC.html#_13-关系运算符" class="sidebar-link">13.关系运算符</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/06.Tools/05.linux/Part03.Shell%E8%84%9A%E6%9C%AC.html#_14-printf格式化打印" class="sidebar-link">14.printf格式化打印</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/06.Tools/05.linux/Part03.Shell%E8%84%9A%E6%9C%AC.html#✅makefile脚本和shell脚本对比" class="sidebar-link">✅makefile脚本和shell脚本对比</a><ul class="sidebar-sub-headers"></ul></li></ul></div></li></ul> </div> <div class="page"> <div class="content"><h1 id="✅linux-shell使用笔记"><a href="#✅linux-shell使用笔记" class="header-anchor">#</a> ✅Linux shell使用笔记</h1> <h2 id="_01-判断的条件"><a href="#_01-判断的条件" class="header-anchor">#</a> 01.判断的条件</h2> <h3 id="格式1"><a href="#格式1" class="header-anchor">#</a> 格式1</h3> <ul><li><p>都使用</p></li> <li><div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token punctuation">[</span><span class="token punctuation">]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li> <li><p>使用</p></li></ul> <h3 id="格式2"><a href="#格式2" class="header-anchor">#</a> 格式2</h3> <ul><li>使用test命令</li></ul> <h2 id="_02-每次的习惯"><a href="#_02-每次的习惯" class="header-anchor">#</a> 02.每次的习惯</h2> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token shebang important">#!/bin/bash</span>
<span class="token assign-left variable">var</span><span class="token operator">=</span><span class="token string">&quot;124&quot;</span>
<span class="token builtin class-name">echo</span> <span class="token variable">$var</span>

<span class="token assign-left variable">ttt</span><span class="token operator">=</span><span class="token number">123</span>
<span class="token assign-left variable">temp</span><span class="token operator">=</span><span class="token number">123</span>

<span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token variable">$ttt</span> <span class="token operator">==</span> <span class="token variable">$temp</span> <span class="token punctuation">]</span>
<span class="token keyword">then</span> 
        <span class="token builtin class-name">echo</span> <span class="token string">&quot;Yes&quot;</span>
<span class="token keyword">else</span>
        <span class="token builtin class-name">echo</span> <span class="token string">&quot;No&quot;</span>
<span class="token keyword">fi</span>

<span class="token builtin class-name">echo</span> <span class="token string">&quot;成功&quot;</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><h2 id="_03-测试环境"><a href="#_03-测试环境" class="header-anchor">#</a> 03.测试环境</h2> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token function">sh</span> demo.sh
上面的可能会出现情况：demo.sh: <span class="token number">7</span>: <span class="token punctuation">[</span>: <span class="token number">123</span>: unexpected operator
原因：linux下能正常执行的脚本，在ubuntu系统中都会编译报错，因为二者使用的解释器不一同导致

Linux：.sh

ubuntu:.dash

因为ubuntu默认的sh是连接到dash的,又因为dash跟bash的不兼容所以出错了.执行时可以把sh换成bash文件名.sh来执行.成功.dash是什么东西,查了一下,应该也是一种shell,貌似用户对它的诟病颇多

解决办法：将Ubuntu解释器修改默认连接到bash

<span class="token function">sudo</span> dpkg-reconfigure dash
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><p>以下的才是OK的（测试习惯）</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token function">bash</span> demo.sh
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>或者用方法</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token function">chmod</span> +x demo.sh
./demo.sh
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h2 id="_04-常见shell错误"><a href="#_04-常见shell错误" class="header-anchor">#</a> 04.常见shell错误</h2> <p>SHELL编程提示：行12: 语法错误: 未预期的文件结尾</p> <h2 id="_05-常见命令"><a href="#_05-常见命令" class="header-anchor">#</a> 05.常见命令</h2> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token builtin class-name">read</span> 
<span class="token builtin class-name">echo</span>
<span class="token builtin class-name">printf</span>
<span class="token builtin class-name">test</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h2 id="_06-if的使用"><a href="#_06-if的使用" class="header-anchor">#</a> 06.if的使用</h2> <p>格式为</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token keyword">if</span> 判断
<span class="token keyword">then</span>
	内容
<span class="token keyword">elif</span> 判断
<span class="token keyword">then</span> 
	内容
<span class="token keyword">else</span>
	内容
<span class="token keyword">fi</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token shebang important">#!/bin/bash</span>
<span class="token assign-left variable">a</span><span class="token operator">=</span><span class="token number">123</span>
<span class="token assign-left variable">b</span><span class="token operator">=</span><span class="token number">123</span>
<span class="token assign-left variable">c</span><span class="token operator">=</span><span class="token number">123</span>
<span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token variable">$a</span> <span class="token operator">==</span> <span class="token variable">$b</span> <span class="token punctuation">]</span>
<span class="token keyword">then</span> 
	<span class="token builtin class-name">echo</span> <span class="token string">&quot;equal&quot;</span>
<span class="token keyword">elif</span> <span class="token punctuation">[</span> <span class="token variable">$a</span> -gt <span class="token variable">$c</span> <span class="token punctuation">]</span>
<span class="token keyword">then</span> 
	<span class="token builtin class-name">echo</span> <span class="token string">&quot;greater&quot;</span>
<span class="token keyword">else</span>
	<span class="token builtin class-name">echo</span> <span class="token string">&quot;Bye&quot;</span>
	
<span class="token builtin class-name">echo</span> <span class="token string">&quot;end&quot;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><p>上面出错：</p> <p>SHELL编程提示：行12: 语法错误: 未预期的文件结尾</p> <p>原因：Linux的shell难以定位真正的位置</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token shebang important">#!/bin/bash</span>
<span class="token assign-left variable">a</span><span class="token operator">=</span><span class="token number">123</span>
<span class="token assign-left variable">b</span><span class="token operator">=</span><span class="token number">123</span>
<span class="token assign-left variable">c</span><span class="token operator">=</span><span class="token number">123</span>
<span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token variable">$a</span> <span class="token operator">==</span> <span class="token variable">$b</span> <span class="token punctuation">]</span>
<span class="token keyword">then</span> 
	<span class="token builtin class-name">echo</span> <span class="token string">&quot;equal&quot;</span>
<span class="token keyword">elif</span> <span class="token punctuation">[</span> <span class="token variable">$a</span> -gt <span class="token variable">$c</span> <span class="token punctuation">]</span>
<span class="token keyword">then</span> 
	<span class="token builtin class-name">echo</span> <span class="token string">&quot;greater&quot;</span>
<span class="token keyword">else</span>
	<span class="token builtin class-name">echo</span> <span class="token string">&quot;Bye&quot;</span>
<span class="token keyword">fi</span>		<span class="token comment">#这个地方出错</span>
<span class="token builtin class-name">echo</span> <span class="token string">&quot;end&quot;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><h2 id="_07-for的使用"><a href="#_07-for的使用" class="header-anchor">#</a> 07.for的使用</h2> <h3 id="格式1-2"><a href="#格式1-2" class="header-anchor">#</a> 格式1</h3> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token keyword">for</span> 变量名字 <span class="token keyword">in</span> 几个变量
<span class="token keyword">do</span>
	内容，注意，for和while都用到了do和done
<span class="token keyword">done</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token keyword">for</span> <span class="token for-or-select variable">var</span> <span class="token keyword">in</span> <span class="token number">111</span> first <span class="token number">222</span>
<span class="token keyword">do</span>
	<span class="token builtin class-name">echo</span> <span class="token variable">$var</span>
<span class="token keyword">done</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h3 id="格式2-2"><a href="#格式2-2" class="header-anchor">#</a> 格式2</h3> <ul><li>欧拉筛法</li></ul> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token operator">!</span> /bin/bash
<span class="token builtin class-name">echo</span> BeginOutput
<span class="token comment">#注释：表示Primer数组中素数的个数</span>
<span class="token assign-left variable">countPrimer</span><span class="token operator">=</span><span class="token number">0</span>

<span class="token comment">#注释1：初始化标记数组，0表示i是素数——当然，我们说的除掉0和1</span>
<span class="token comment">#注释2：Primer数组中存素数，从0号位置存起</span>
<span class="token keyword">for</span> <span class="token variable"><span class="token punctuation">((</span>i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> i<span class="token operator">&lt;</span><span class="token number">100005</span><span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">))</span></span>
<span class="token keyword">do</span>
	TagArray<span class="token punctuation">[</span><span class="token variable">$i</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">0</span>
	Primer<span class="token punctuation">[</span><span class="token variable">$i</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">0</span>
<span class="token keyword">done</span>

<span class="token keyword">for</span> <span class="token variable"><span class="token punctuation">((</span>i<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">;</span> i<span class="token operator">&lt;</span><span class="token number">100005</span><span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">))</span></span>
<span class="token keyword">do</span>
	<span class="token assign-left variable">tag</span><span class="token operator">=</span><span class="token variable">${TagArray<span class="token punctuation">[</span>$i<span class="token punctuation">]</span>}</span>
	<span class="token keyword">if</span> <span class="token variable"><span class="token punctuation">((</span>tag<span class="token operator">==</span><span class="token number">0</span><span class="token punctuation">))</span></span>
	<span class="token keyword">then</span>
		Primer<span class="token punctuation">[</span><span class="token variable">$countPrimer</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token variable">$i</span>
		<span class="token variable"><span class="token punctuation">((</span><span class="token operator">++</span>countPrimer<span class="token punctuation">))</span></span>
		<span class="token builtin class-name">echo</span> <span class="token variable">$i</span>
	<span class="token keyword">fi</span>

	<span class="token keyword">for</span> <span class="token variable"><span class="token punctuation">((</span>loop<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> <span class="token punctuation">((</span>loop<span class="token operator">&lt;</span>countPrimer<span class="token punctuation">))</span></span> <span class="token operator">&amp;&amp;</span> <span class="token variable"><span class="token punctuation">((</span>i<span class="token operator">*</span>Primer[loop]<span class="token operator">&lt;</span><span class="token number">100005</span><span class="token punctuation">))</span></span> <span class="token punctuation">;</span> ++loop<span class="token punctuation">))</span>
	<span class="token keyword">do</span>
		<span class="token assign-left variable">temp</span><span class="token operator">=</span><span class="token variable">${Primer<span class="token punctuation">[</span>$loop<span class="token punctuation">]</span>}</span>
		TagArray<span class="token punctuation">[</span><span class="token variable"><span class="token variable">$((</span>i<span class="token operator">*</span>temp<span class="token variable">))</span></span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">1</span>
		<span class="token assign-left variable">tempOther</span><span class="token operator">=</span><span class="token variable"><span class="token variable">$((</span>i<span class="token operator">%</span>temp<span class="token variable">))</span></span>
		<span class="token keyword">if</span> <span class="token variable"><span class="token punctuation">((</span>tempOther<span class="token operator">==</span><span class="token number">0</span><span class="token punctuation">))</span></span>
		<span class="token keyword">then</span> 
			<span class="token builtin class-name">break</span>
		<span class="token keyword">fi</span>
	<span class="token keyword">done</span>
<span class="token keyword">done</span>

<span class="token builtin class-name">echo</span> <span class="token assign-left variable">countPrimer</span><span class="token operator">=</span>
<span class="token builtin class-name">echo</span> <span class="token variable">$countPrimer</span>
<span class="token builtin class-name">echo</span> -----------------

<span class="token builtin class-name">echo</span> EndOutput
<span class="token comment">#for i in {0..100005}</span>
<span class="token comment">#do</span>
<span class="token comment">#	echo ${TagArray[$i]}</span>
<span class="token comment">#done</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br></div></div><h2 id="_08-while的使用"><a href="#_08-while的使用" class="header-anchor">#</a> 08.while的使用</h2> <h3 id="格式"><a href="#格式" class="header-anchor">#</a> 格式</h3> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token keyword">while</span> 判断条件
<span class="token keyword">do</span>
	内容，注意，for和while都用到了do和done
<span class="token keyword">done</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token assign-left variable">var</span><span class="token operator">=</span><span class="token number">11</span>
<span class="token keyword">while</span> <span class="token punctuation">[</span> <span class="token variable">$var</span> -gt <span class="token number">1</span> <span class="token punctuation">]</span>
<span class="token keyword">do</span>
	<span class="token builtin class-name">echo</span> <span class="token variable">$var</span>
<span class="token keyword">done</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="无限循环"><a href="#无限循环" class="header-anchor">#</a> 无限循环</h3> <p>无限循环
无限循环语法格式：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token keyword">while</span> <span class="token builtin class-name">:</span>
<span class="token keyword">do</span>
    <span class="token builtin class-name">command</span>
<span class="token keyword">done</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>或者</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token keyword">while</span> <span class="token boolean">true</span>
<span class="token keyword">do</span>
    <span class="token builtin class-name">command</span>
<span class="token keyword">done</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>或者</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token keyword">for</span> <span class="token variable"><span class="token punctuation">((</span> <span class="token punctuation">;</span> <span class="token punctuation">;</span> <span class="token punctuation">))</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="_09-until循环（有点像c语言下的do-while循环）"><a href="#_09-until循环（有点像c语言下的do-while循环）" class="header-anchor">#</a> 09.until循环（有点像C语言下的do while循环）</h2> <p>until 循环执行一系列命令直至条件为 true 时停止。</p> <p>until 循环与 while 循环在处理方式上刚好相反。</p> <p>一般 while 循环优于 until 循环，但在某些时候—也只是极少数情况下，until 循环更加有用。</p> <p>until 语法格式:</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token keyword">until</span> condition
<span class="token keyword">do</span>
    <span class="token builtin class-name">command</span>
<span class="token keyword">done</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h2 id="_10-case多选择（类似于c语言中的switch选择）"><a href="#_10-case多选择（类似于c语言中的switch选择）" class="header-anchor">#</a> 10.case多选择（类似于C语言中的switch选择）</h2> <p>case ... esac</p> <p><strong>case ... esac</strong> 为多选择语句，与其他语言中的 switch ... case 语句类似，是一种多分枝选择结构，每个 case 分支用右圆括号开始，用两个分号 <strong>;;</strong> 表示 break，即执行结束，跳出整个 case ... esac 语句，esac（就是 case 反过来）作为结束标记。</p> <p>可以用 case 语句匹配一个值与一个模式，如果匹配成功，执行相匹配的命令。</p> <p><strong>case ... esac</strong> 语法格式如下：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token keyword">case</span> 值 <span class="token keyword">in</span>
模式1<span class="token punctuation">)</span>
    command1
    command2
    <span class="token punctuation">..</span>.
    commandN
    <span class="token punctuation">;</span><span class="token punctuation">;</span>
模式2）
    command1
    command2
    <span class="token punctuation">..</span>.
    commandN
    <span class="token punctuation">;</span><span class="token punctuation">;</span>
<span class="token keyword">esac</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><h2 id="_11-break和continue"><a href="#_11-break和continue" class="header-anchor">#</a> 11.break和continue</h2> <h2 id="_12-shell-基本运算符（记忆、强化，对比汇编语句）"><a href="#_12-shell-基本运算符（记忆、强化，对比汇编语句）" class="header-anchor">#</a> 12.Shell 基本运算符（记忆、强化，对比汇编语句）</h2> <ul><li><a href="https://www.runoob.com/linux/linux-shell-basic-operators.html" target="_blank" rel="noopener noreferrer">参考网站<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul> <p>Shell 和其他编程语言一样，支持多种运算符，包括：</p> <ul><li>算数运算符</li> <li>关系运算符</li> <li>布尔运算符</li> <li>字符串运算符</li> <li>文件测试运算符</li></ul> <p>原生bash不支持简单的数学运算，但是可以通过其他命令来实现，例如 awk 和 expr，expr 最常用。</p> <p>expr 是一款表达式计算工具，使用它能完成表达式的求值操作。</p> <p>例如，两个数相加(**注意使用的是反引号 *<em>`\ 而不是单引号 *</em>'**)：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>运算符	说明	举例
+	加法	<span class="token variable"><span class="token variable">`</span><span class="token function">expr</span> $a + $b<span class="token variable">`</span></span> 结果为 <span class="token number">30</span>。
-	减法	<span class="token variable"><span class="token variable">`</span><span class="token function">expr</span> $a - $b<span class="token variable">`</span></span> 结果为 -10。
*	乘法	<span class="token variable"><span class="token variable">`</span><span class="token function">expr</span> $a <span class="token punctuation">\</span>* $b<span class="token variable">`</span></span> 结果为  <span class="token number">200</span>。
/	除法	<span class="token variable"><span class="token variable">`</span><span class="token function">expr</span> $b / $a<span class="token variable">`</span></span> 结果为 <span class="token number">2</span>。
%	取余	<span class="token variable"><span class="token variable">`</span><span class="token function">expr</span> $b % $a<span class="token variable">`</span></span> 结果为 <span class="token number">0</span>。
<span class="token operator">=</span>	赋值	<span class="token assign-left variable">a</span><span class="token operator">=</span><span class="token variable">$b</span> 将把变量 b 的值赋给 a。
<span class="token operator">==</span>	相等。用于比较两个数字，相同则返回 true。	<span class="token punctuation">[</span> <span class="token variable">$a</span> <span class="token operator">==</span> <span class="token variable">$b</span> <span class="token punctuation">]</span> 返回 false。
<span class="token operator">!=</span>	不相等。用于比较两个数字，不相同则返回 true。	<span class="token punctuation">[</span> <span class="token variable">$a</span> <span class="token operator">!=</span> <span class="token variable">$b</span> <span class="token punctuation">]</span> 返回 true。
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token assign-left variable">var</span><span class="token operator">=</span><span class="token number">1</span>

<span class="token keyword">while</span> <span class="token boolean">true</span>
<span class="token keyword">do</span> 
        <span class="token builtin class-name">echo</span> <span class="token variable">$var</span>
        <span class="token assign-left variable">var</span><span class="token operator">=</span><span class="token variable"><span class="token variable">`</span><span class="token function">expr</span> $var + <span class="token number">1</span> <span class="token variable">`</span></span>
<span class="token keyword">done</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h2 id="_13-关系运算符"><a href="#_13-关系运算符" class="header-anchor">#</a> 13.关系运算符</h2> <p>关系运算符只支持数字，不支持字符串，除非字符串的值是数字。</p> <p>下表列出了常用的关系运算符，假定变量 a 为 10，变量 b 为 20：</p> <table><thead><tr><th style="text-align:left">运算符</th> <th style="text-align:left">说明</th> <th style="text-align:left">举例</th></tr></thead> <tbody><tr><td style="text-align:left">-eq</td> <td style="text-align:left">检测两个数是否相等，相等返回 true。</td> <td style="text-align:left">[ $a -eq $b ] 返回 false。</td></tr> <tr><td style="text-align:left">-ne</td> <td style="text-align:left">检测两个数是否不相等，不相等返回 true。</td> <td style="text-align:left">[ $a -ne $b ] 返回 true。</td></tr> <tr><td style="text-align:left">-gt</td> <td style="text-align:left">检测左边的数是否大于右边的，如果是，则返回 true。</td> <td style="text-align:left">[ $a -gt $b ] 返回 false。</td></tr> <tr><td style="text-align:left">-lt</td> <td style="text-align:left">检测左边的数是否小于右边的，如果是，则返回 true。</td> <td style="text-align:left">[ $a -lt $b ] 返回 true。</td></tr> <tr><td style="text-align:left">-ge</td> <td style="text-align:left">检测左边的数是否大于等于右边的，如果是，则返回 true。</td> <td style="text-align:left">[ $a -ge $b ] 返回 false。</td></tr> <tr><td style="text-align:left">-le</td> <td style="text-align:left">检测左边的数是否小于等于右边的，如果是，则返回 true。</td> <td style="text-align:left">[ $a -le $b ] 返回 true。</td></tr></tbody></table> <h2 id="_14-printf格式化打印"><a href="#_14-printf格式化打印" class="header-anchor">#</a> 14.printf格式化打印</h2> <ul><li>shell的printf和C语言中的不是一个东西，但是效果相似</li></ul> <h2 id="✅makefile脚本和shell脚本对比"><a href="#✅makefile脚本和shell脚本对比" class="header-anchor">#</a> ✅makefile脚本和shell脚本对比</h2> <p>我们测试</p> <div class="language-sh line-numbers-mode"><pre class="language-text"><code>[root@iZwz9ivqsnyj6hi71t3pamZ ~]# make -v
GNU Make 4.2.1
Built for x86_64-redhat-linux-gnu
Copyright (C) 1988-2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later &lt;http://gnu.org/licenses/gpl.html&gt;
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><blockquote><ul><li>make是打包工具，对应的脚本文件是makefile</li> <li>shell是操作系统的用户界面,对应的脚本文件是sh脚本</li></ul></blockquote> <p>一个是专用工具（make）
一个是通用工具（shell）</p> <blockquote><ul><li>不过你高兴高兴倒是可以手写shell脚本编译你的项目 , 但是makefile是设计来完成编译工作的 , 它有许多你用shell脚本写起来很麻烦但是很有用的功能 , 比如增量编译 , 多线程编译之类的</li></ul></blockquote></div> <div class="page-edit"><!----> <!----></div> <!----> </div> <!----></div></div>
    <script src="/assets/js/app.c70e21ad.js" defer></script><script src="/assets/js/149.ffc835b5.js" defer></script>
  </body>
</html>
